import * as appConfig from '../../app-config.js';
var app = getApp()
Page({
  data: {
    topCurrentId:'',
    leftCurrentId:'',
    settlement:'卖货结算',
    leftBtn:[],
    list:[],
    goodsType:'2',
    addImg:'../../image/add.png',
    priceName:'零售价',
    agentId:'',
    noData:false
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
      this.getGoodsTypeList(res.data.agentId)
      this.getGoodsList(1,'')
    })
  },
  //商品种类选择事件
  bindLeftBtn(e){
    let type = e.currentTarget.dataset.type
    console.log(type)
    this.setData({
      leftCurrentId:e.currentTarget.dataset.id,
      goodsType:type
    })
    this.getGoodsList(1,'')
  },
  //结算
  bindToSettlement(){
      wx.navigateTo({
        url: 'settlement?type=1'
      })
  },
  //商品详情
  bindToDetail(e){
    wx.navigateTo({
      url:'goodsDetail?type=1&goodsType='+this.data.goodsType+'&id='+e.currentTarget.dataset.id+'&price='+e.currentTarget.dataset.price
    })
  },
  //获取商品列表
  getGoodsList(pageNum,searchKey){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum,
      searchKey,
    }
    let list = []
    app.request('wx_app/mall/client/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.name,
            price:item.price,
            url:item.url,
            id:item.id
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        res.errorMsg(res.message)
      }
    })
    console.log(this.data.list.lenght)
    // if (this.data.list.lenght==0) {
    //   this.setData({
    //     noData:true
    //   })
    // }else{
    //   this.setData({
    //     noData:false
    //   })
    // }
  },
  //获取商品种类
  getGoodsTypeList(agentId){
    var that = this
    var leftBtn = this.data.leftBtn
    var data = {
      agentId,
      requestType:'client'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res.data.product_types[0].id)
      let btnList = res.data.product_types
      //默认种类
      that.setData({
        goodsType:res.data.product_types[0].id
      })
      btnList.forEach(item=>{
        leftBtn.push({name:item.name,id:item.id})
        that.setData({
          leftBtn
        })
      })
    })
  }
})